<?php

namespace app\common\service;
use think\Cache;
use think\Exception;

/**
 * 萤石云api管理
 * Class EzuikitService
 * @package app\common\service
 */
class EzuikitService
{
    /**
     * 获取设备token
     * @return mixed
     * @throws ApiServiceException
     */
    static function getToken(){
        $token      = Cache::get("EzuikitToken");
        if ($token) return $token;
        $config     = config("ezuikit");

        $reslult    = curlPost($config["token_url"], $config["config"]);
        if ($reslult && $reslult["code"] == "200") {
            $time   = 5 * 24 * 3600;//有效期5天
            Cache::set("EzuikitToken", $reslult["data"]["accessToken"], $time);
            return $reslult["data"]["accessToken"];
        }
        return "";
//        return abort(202,"token获取失败");
    }


    /**
     * 获取截图
     * @param $deviceSerial string 设备序列号
     * @param $channelNo int 通道号
     * @return mixed
     * @throws ApiServiceException
     */
    static function capture($deviceSerial,$channelNo){
        $token                  = self::getToken();
        $config                 = config("ezuikit");
        $data["accessToken"]    = $token;
        $data["deviceSerial"]   = $deviceSerial;
        $data["channelNo"]      = $channelNo;
        $reslult                = curlPost($config["capture"], $data);
        if ($reslult && $reslult["code"] == 200){
            $url                = $reslult["data"]["picUrl"];
            return $url;
        }
        return $reslult["msg"];
    }


    /**
     * 获取视频地址
     * @param $deviceSerial string 设备序列号
     * @param $channelNo int 通道号
     * @param int $startTime 开始时间
     * @param int $stopTime 结束时间
     * @return mixed|string
     * @throws ApiServiceException
     */
    static function getVideoPath($deviceSerial,$channelNo,$startTime = 0,$stopTime = 0){
        $token                  = self::getToken();
        $config                 = config("Ezuikit");
        $data["accessToken"]    = $token;
        $data["deviceSerial"]   = $deviceSerial;
        $data["channelNo"]      = $channelNo;
        $data["protocol"]       = 4;
        if ($startTime && $stopTime){
            $data["type"]       = 2;
            $data["startTime"]  = $startTime;
            $data["stopTime"]   = $stopTime;
        }
        $reslult                = curlPost($config["address_url"], $data);
        if ($reslult && $reslult["code"] == 200){
            return  $dataList   = $reslult["data"]["url"] ?? '';
        }
        return $reslult["msg"];
    }


    /**
     * 镜头控制
     * @param $deviceSerial string 设备序列号
     * @param $channelNo int 通道号
     * @param $direction int 操作命令：0-上，1-下，2-左，3-右，4-左上，5-左下，6-右上，7-右下，8-放大，9-缩小，10-近焦距，11-远焦距
     * @param $speed int 云台速度：0-慢，1-适中，2-快，海康设备参数不可为0
     * @return mixed
     * @throws ApiServiceException
     */
    static function moveControl($deviceSerial,$channelNo,$direction,$speed = 1){
        $token                  = self::getToken();
        $config                 = config("ezuikit");
        $data["accessToken"]    = $token;
        $data["deviceSerial"]   = $deviceSerial;
        $data["channelNo"]      = $channelNo;
        $data["direction"]      = $direction;
        $data["speed"]          = $speed;
        $reslult                = curlPost($config["control"], $data);
        if ($reslult && $reslult["code"] == 200){
            return $reslult;
        }
        return $reslult["msg"];
    }


    /**
     * 镜头控制停止
     * @param $deviceSerial string 设备序列号
     * @param $channelNo int 通道号
     * @param $direction int 操作命令：0-上，1-下，2-左，3-右，4-左上，5-左下，6-右上，7-右下，8-放大，9-缩小，10-近焦距，11-远焦距
     * @return mixed
     * @throws ApiServiceException
     */
    static function stopControl($deviceSerial,$channelNo,$direction){
        $token                  = self::getToken();
        $config                 = config("ezuikit");
        $data["accessToken"]    = $token;
        $data["deviceSerial"]   = $deviceSerial;
        $data["channelNo"]      = $channelNo;
        $data["direction"]      = $direction;
        $reslult                = curlPost($config["stop"], $data);
        if ($reslult && $reslult["code"] == 200){
            return $reslult;
        }
        return $reslult["msg"];
    }


    /**
     * 镜像翻转
     * @param $deviceSerial string 设备序列号
     * @param $channelNo int 通道号
     * @param $command int 镜像方向：0-上下, 1-左右, 2-中心
     * @return mixed
     * @throws ApiServiceException
     */
    static function mirror($deviceSerial,$channelNo,$command = 0){
        $token                  = self::getToken();
        $config                 = config("ezuikit");
        $data["accessToken"]    = $token;
        $data["deviceSerial"]   = $deviceSerial;
        $data["channelNo"]      = $channelNo;
        $data["command"]        = $command;
        $reslult                = curlPost($config["mirror"], $data);
        if ($reslult && $reslult["code"] == 200){
            return $reslult;
        }
        return $reslult["msg"];
    }





}
